<?php
// +----------------------------------------------------------------------
// | Created by Sublime: [ Conqweal ]
// +----------------------------------------------------------------------
// | Copyright (c) 2021~20223[chenjia] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.99dn.cn
// +----------------------------------------------------------------------
// | Author: Conqweal <150672834@qq.com>
// +----------------------------------------------------------------------
// | Date: 2021/12/22 0026
// +----------------------------------------------------------------------
// | Description:邮箱验证码
// +----------------------------------------------------------------------
namespace app\api\controller;

use app\Api;
use app\api\library\Ems as Emslib;
use app\api\model\User;
use think\facade\Db;
use think\facade\Cache;
use think\facade\Validate;
use app\ApiController;
use support\Request;
use Webman\Captcha\CaptchaBuilder;
class EmailCaptchaController extends ApiController
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

        

    }

    /**
     * 发送验证码
     *
     * @ApiMethod (POST)
     * @param string $email 邮箱
     * @param string $event 事件名称
     */
    public function send()
    {
        //时间触发

        $email   = $this->request->post("email");
        $event   = $this->request->post("event");
        $captcha = $this->request->post("captcha");
        $event   = $event ? $event : 'register';

        $last = Emslib::get($email, $event);       
        if ($last && time() - strtotime($last['createtime']) < 60) {
            return $this->error(__('Send frequently'));
        }

        if ($event) {

            if (in_array($event, ['resetpwd', 'authen']) && !$this->captchaCheck()) {
                    
                return $this->error(__('captcha is incorrect'));
            };

            $userinfo = User::where('email',$email)->find();

 

            if ($event == 'register' && $userinfo) {
                //已被注册
                return $this->error(__('Registered'));
            } elseif (in_array($event, ['changeemail']) && $userinfo) {
                //被占用
                return $this->error(__('Occupied'));
            } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
                //未注册
                return $this->error(__('unregistered'));
            }

        }
       
        $ret = Emslib::send($email, null, $event);
      
        if ($ret) {
            return $this->success(__('Sent Successfully'));
        } else {
            return $this->error(__('send failed'));
        }
    }


    private function captchaCheck(){
        // 获取post请求中的captcha字段
        $captcha = $this->request->post('captcha');
        // 对比session中的captcha值
        if (strtolower($captcha) !== $this->request->session()->get('captcha')) {
            return false;
        }
        return true;
    }
 


    /**
     * 检测验证码
     *
     * @ApiMethod (POST)
     * @param string $email   邮箱
     * @param string $event   事件名称
     * @param string $captcha 验证码
     */
    public function check()
    {
        $email   = $this->request->post("email");
        $event   = $this->request->post("event");
        $event   = $event ? $event : 'register';
        $captcha = $this->request->post("captcha");

        if ($event) {
            $userinfo = User::getByEmail($email);
            if ($event == 'register' && $userinfo) {
                //已被注册
                return $this->error(__('Registered'));
            } elseif (in_array($event, ['changeemail']) && $userinfo) {
                //被占用
                return $this->error(__('Occupied'));
            } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
                //未注册
                return $this->error(__('unregistered'));
            }
        }
        $ret = Emslib::check($email, $captcha, $event);
        if ($ret) {
            return $this->success(__('ok'));
        } else {
            return $this->error(__('captcha is incorrect'));
        }
    }
}
